﻿using System;
using System.Web;
using Ninject;

namespace Nydus.WebForms.FrontendModel
{
    public class CreatePresenterModule : IHttpModule
    {
        public String ModuleName
        {
            get { return "CreatePresenterModule"; }
        }

        // In the Init function, register for HttpApplication 
        // events by adding your handlers.
        public void Init(HttpApplication application)
        {
            this.Application = application;
            application.PreRequestHandlerExecute += application_PreRequestHandlerExecute;
        }

        [Inject]
        public IPresenterFactory PresenterFactory { get; set; }

        void application_PreRequestHandlerExecute(object sender, EventArgs e)
        {
            if (Application.Context.Handler is BasePage)
            {
                var page = (BasePage)Application.Context.Handler;
                CreatePresenter(page);
            }
        }

        /// <summary>
        /// Creates the presenter.
        /// </summary>
        private void CreatePresenter(BasePage page)
        {
            PresenterBuilder.Current.GetPresenter(new HttpContextWrapper(Application.Context), page);
        }

        private HttpApplication Application { get; set; }

        public void Dispose()
        {
            
        }
    }
}
